﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WisSensorNLibLib;
namespace Personal_Control
{
    public partial class Form1 : Form
    {
        DataBase db;
        WisSensorN lector;
        String[] fingers;
        String currentFinger = "";        
        String errors = "";
        String mgs = "";
        public Form1()
        {
            this.initialCommands();
        }
        /*
         * Se inicializa la base de datos y trata de abrir una conexion. En caso de exito, 
         */
        public void initialCommands()
        {
            /* Conexion a la base de datos*/
            db = new DataBase();
            if (db.conectDB())
            {
                InitializeComponent();
                /* Comprobacion del lector dactilar */
                lector = new WisSensorN();
                lector.Open();
                lector.DataEvent += new _IWisSensorNEvents_DataEventEventHandler(WisObj_DataEvent);
                obtainFingers();
                lector.IdentifyCapture();
                
            
            }
            else
            {
                MessageBox.Show("Se produjo un erro al conectarse con la base de datos.", "Database connection ", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }
        private bool validateData()
        {
            errors = "";
            bool ret=true;
            if (this.nameText.Text.Equals(""))
            {
                errors += "Nombre: es requerido \n";
                ret = false;
            }
            if (this.surnameText.Text.Equals(""))
            {
                errors += "Apellido: es requerido \n";
                ret = false;
            }
            if (this.appText.Text.Equals(""))
            {
                errors += "Cargo: es requerido \n";
                ret = false;
            }
            if (currentFinger.Equals(""))
            {
                errors += "Huella: no digiiltalizada \n";
                ret = false;
            }

            return ret;
        }
        private void cleanAll()
        {
            this.nameText.Text = "";
            this.surnameText.Text = "";
            this.appText.Text = "";
            this.currentFinger = "";
            this.finger_pic.Image = null;
            lector.IdentifyCapture();
        }
        private void obtainFingers()
        {
            fingers = this.db.fingerPrints();
        }
        /*********************************************************************
         *                         LISTENERS                                 *
         * *******************************************************************
         */
        private void checkPanel_CheckedChanged(object sender, EventArgs e)
        {
            if (checkPanel.Checked)
            {
                newPanel.Enabled = true;
            }
            else
            {
                newPanel.Enabled = false;
            }
        }
        private void buttonCapture_Click(object sender, EventArgs e)
        {
            lector.StopCapture();
            this.finger_pic.Image = null;
            lector.SetDisplay((int)finger_pic.Handle);
            lector.StartEnroll();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (validateData())
            {
                this.db.addPersonal(this.nameText.Text, this.surnameText.Text, this.appText.Text, this.currentFinger);
                this.cleanAll();
                this.obtainFingers();                
            }
            else
            {
                MessageBox.Show( this.errors,"Stop", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
        }
        private void close(object sender, FormClosingEventArgs e)
        {
            
            this.db.disconnectDB();
            this.lector.StopCapture();
            this.lector.Close();
            this.Dispose();
            this.Close();
        }


        /*********************************************************************
         *           SECCION PARA LAS FUNCIONES DEL LECTOR                   *
         * *******************************************************************
         */
        private void WisObj_DataEvent(WisSensorNLibLib.DATA data, string str)
        {
            switch (data)
            {
                case DATA.DATA_ENROLL:
                    this.currentFinger = str;
                    break;

                case DATA.DATA_IDENTIFY_CAPTURE:
                    int nMatched;
                    nMatched = lector.Identify(str, fingers);
                    if (nMatched < 0)
                    {
                        mgs = "No valid finger matched!!";
                        Console.Beep(5000, 500);
                        Console.Beep(5000, 500);
                        
                    }
                    else
                    {
                        Console.Beep(5000, 500);

                        mgs = this.db.addRegister(nMatched);
                       
                    }
                    this.mostrarNotificiacion("Registro", mgs, ToolTipIcon.Info);
                    break;

                case DATA.DATA_VERIFY_CAPTURE:
                    break;
            }
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void comenzar(object sender, EventArgs e)
        {
            lector.IdentifyCapture();
        }
        private void mostrarNotificiacion(String title, String msg, ToolTipIcon icon)
        {
            popup.BalloonTipIcon = icon;
            popup.BalloonTipText = msg;
            popup.BalloonTipTitle = title;

            popup.ShowBalloonTip(1000);

        }
       
       
    }
}
